//
//  CZStatusRetweetView.swift
//  iOSWeibo10
//
//  Created by itcast on 16/5/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class CZStatusRetweetView: UIView {
    
    /// 被转发的模型
    var retweetStatus: CZStatus? {
        didSet {
            // 被转发的微博不一定有值
            // 给retweetContentLabel赋值
            if retweetStatus == nil {
                retweetContentLabel.text = "没有被转发内容，我是被复用的"
            } else {
                // 设置被转发微博内容
                // @被转发微博用户的名称: 被转发微博的内容
                let name = retweetStatus?.user?.screen_name ?? "未知用户名"
                let text = retweetStatus?.text ?? ""
                retweetContentLabel.text = "@\(name): \(text)"
            }
            
            // 设置被转发的图片图片,内部会自动更新大小
            retweetPictureView.pictureURLs = retweetStatus?.pictureURLs
            
            // 被转发微博图片数量为0
            // 配图没有图片重新更新配图的顶部
            retweetPictureView.snp_updateConstraints { (make) -> Void in
                let margin = retweetStatus?.pictureURLs?.count == 0 ? 0 : CellMargin
                make.top.equalTo(retweetContentLabel.snp_bottom).offset(margin)
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    private func prepareUI() {
//        backgroundColor = UIColor.brownColor()
//        retweetPictureView.backgroundColor = UIColor.magentaColor()
        
        // 添加控件
        addSubview(retweetContentLabel)
        addSubview(retweetPictureView)
        
        // 添加约束, 注意如果添加约束的这个控件没有父控件,添加不了约束
        /// 被转发的微博文本
        retweetContentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(CellMargin)
            make.top.equalTo(self).offset(CellMargin)
        }
        
        /// 被转发的配图
        retweetPictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(retweetContentLabel)
            make.top.equalTo(retweetContentLabel.snp_bottom).offset(CellMargin)
            make.size.equalTo(CGSize(width: 200, height: 200))
        }
        
        /// 底部约束
        /// 被转发底部参照配图底部偏移8
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(retweetPictureView).offset(CellMargin)
        }
    }
    
    // MARK: - 懒加载
    /// 被转发微博的内容
    private lazy var retweetContentLabel: UILabel = {
        let label = UILabel()
        
        // 设置
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(CellStatusFontSize)
        label.numberOfLines = 0
        
        // 最大宽度
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * CellMargin
        
        label.text = "被转发微博的内容被转发微博的内容被转发微博的内容被转发微博的内容被转发微博的内容被转发微博的内容被转发微博的内容被转发微博的内容被转发微博的内容"
        
        return label
    }()
    
    /// 被转发的配图
    private lazy var retweetPictureView: CZStatusPictureView = CZStatusPictureView()
}
